package Solution_5;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Puke implements Comparable{

    private  static  String color [] ={"黑桃","梅花","红桃","方块"};
    private  static  String number [] ={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};


    private static String puke [] =new String[54]; // 牌

    private static  Map<String,Integer> mnum = new HashMap<>(); // 保存判断牌的大小
    private static Map<String,Boolean> fapai = new HashMap<>(); // 发牌容器 没发就false，发了就 true；

    static{
        for(int i = 0; i < 4;i++){
            String str = color[i];
            for(int j = 0; j < 13 ;j++){
                puke[j+i*13] = str + number[j];
            }
        }
        puke[52] = "小王";
        puke[53]="大王";

        //表示没有发牌
       // System.out.println("所有牌");
        for (int i = 0; i < puke.length;i++){
            fapai.put(puke[i],false);
           // System.out.println( " " + puke[i] + " 是" +fapai.get(puke[i]));
        }

        //以number数组索引判断大小
        for (int i = 0; i < number.length;i++){
            mnum.put(number[i],i);
        }
    }

    public Puke(){

    }
    public Puke(String pukeColor){

        this.pukeColor = pukeColor;
        this.pukeNumber = "";
    }
    public Puke(String pukeColor,String pukeNumber){
        this.pukeColor = pukeColor;
        this.pukeNumber = pukeNumber;
    }


    public static Puke choose(){
      //  Collections.shuffle(puke);
        Random random = new Random();
        int idx = random.nextInt(54);
        while (fapai.get(puke[idx])){
            idx = random.nextInt(54);
        }
        fapai.put(puke[idx],true);
        String str = puke[idx];
        /*System.out.println("发了这个牌");
        System.out.println( " " + puke[idx] + " 是" +fapai.get(puke[idx]));*/
        if (str.length()!=2)
          return new Puke(str.substring(0,2),str.substring(2));
        else
            return new Puke(str);
    }

    public String getPukeColor() {
        return pukeColor;
    }

    public void setPukeColor(String pukeColor) {
        this.pukeColor = pukeColor;

    }

    public String getPukeNumber() {
        return pukeNumber;
    }

    public void setPukeNumber(String pukeNumber) {
        this.pukeNumber = pukeNumber;
    }

    private String pukeNumber;
    private String pukeColor;
   // private String allname;


  /*  @Override
    public boolean equals(Object obj) {
        if (obj == null )return false;
        if (obj instanceof Puke){
            return this.pukeColor == ((Puke) obj).pukeColor && this.pukeNumber == ((Puke) obj).pukeNumber;
        }
        return this == obj ;
    }*/

  /*  @Override
    public int hashCode() {
        return pukeNumber.hashCode() + pukeColor.hashCode();
    }*/

    @Override
    public int compareTo(Object o) {
        if (this.getPukeColor().equals("大王")) return  -1;
        if (((Puke)o).getPukeColor().equals("大王")) return 1;
        if (this.getPukeColor().equals("小王")) return -1;
        if (((Puke)o).getPukeColor().equals("小王")) return 1;
        return -this.mnum.get(this.getPukeNumber()) + ((Puke)o).mnum.get(((Puke) o).getPukeNumber());

    }
}
